const router = require('express').Router();
const connection = require('../database/mysql')
const path = require('path')
const sql = require('../routehandles/mysqlhandle')

router.post('/register',async function(req,res)
{
    var params = req.body
    console.log(params)
    let rule = /^1[345789]\d{9}$/  //是否为标准的电话号码
    var reg= /^[\u4E00-\u9FA5A-Za-z0-9_]{3,10}$/   //昵称3-10位，汉字或者字母或者数字或者组合
    let rule1 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/   //6-20位，包含大小写和数字
    var restOfTime = 6
    if(params.username == null || params.username == '')
    {
        return res.json({msg:'请填写电话号码！',result:false})
    }else if (!rule.test(params.username))
    {
        console.log(params.username)
        return res.json({msg:'号码不正确！',result:false})
    }
    else if(params.nickName == '' || params.nickName == null)
    {
        console.log('1111')
        return res.json({msg:'请填写昵称',result:false})
    }else if(!params.nickName.match(reg))
    {
        console.log(params.nickname)
        return res.json({msg:'昵称不合法！',result:false})//后期有时间这里再细化
    }
    else if(params.password == '' || params.password2 == '' || params.password == null || params.password2 == null)
    {
        console.log("密码为空");
        return res.json({msg:'密码不能为空！',result:false})
    }else if(!params.password.match(rule1) && !params.password2.match(rule1))
    {
        return res.json({msg:'密码格式不正确！',result:false})
    }else if(params.password != params.password2)
    {
        console.log('两次密码不一致！')
        return res.json({msg:'两次密码不一致！',result:false})
    }else if(params.verify == null || params.verify == '')
    {
        console.log('为输入验证码')
        return res.json({msg:'请输入验证码',result:false})
    }else if(params.verify != params.code)
    {
        return res.json({msg:'验证码不正确',result:false,last:restOfTime})
    }else if(await getData(params.username))//检测以该用户名为key是否为空
    {
        console.log('1111')
        return res.json({msg:'该号码已被注册，忘记密码？',result:false})
    }else if(params.read == 'false')
    {
        return res.json({msg:'请同意用户协议及隐私政策',result:false})
    }else
    {
        console.log('1111')
        sql.iUAPTable(params.username,params.password,params.nickName,"hasdk")
        return res.json({msg:'注册成功！',result:true})
    }
})

module.exports = router

const getData = async (username) => {
    var xx = await sql.sUAPTable(username)
    console.log(xx)
    var a = (xx.length != 0)
    return a
}